package com.zunipe.chat;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.zunipe.chat.databinding.ActivityLoginBinding;

public class LoginActivity extends AppCompatActivity {
    ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (ChatApplication.getInstance().isChatServiceRunning()) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        String username = ChatApplication.getInstance().getUsername();
        if (!username.isEmpty()) {
            binding.etUsername.setText(username);
        }
        binding.btnLogin.setOnClickListener(v -> {
            String inputUsername = binding.etUsername.getText().toString();
            if (!inputUsername.isEmpty()) {
                ChatApplication.getInstance().setUsername(inputUsername);

                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }
}